//
//  putDynamicDetailVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/7/6.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "putDynamicDetailVC.h"
#import "DFGridImageView.h"
#import "projectCell.h"
#import "PinLunCell.h"
#import "LSReportVCTableViewController.h"
#define UserNickMaxWidth 150
#define UserNickLineHeight 1.0f
#define GridMaxWidth (BodyMaxWidth)*0.85
#define  BodyMaxWidth [UIScreen mainScreen].bounds.size.width - 40 - 3*15
static NSMutableArray *dataArr;
static NSString *projectcell = @"projectcell";
@interface putDynamicDetailVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSInteger type;
    NSInteger page;
    NSDictionary * dataDict;

}
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UITextField *textfiled;

@property (nonatomic, strong) UIImageView *oneImageView;
@property (nonatomic, strong) UIButton *oneImageButton;
@property (nonatomic, strong) UIView *bodyView;
@property (strong, nonatomic) DFGridImageView *gridImageView;
@property (strong, nonatomic) UILabel *zanLabel;
@property (strong, nonatomic) UILabel *shareLabel;
@property (strong, nonatomic) UIButton *zanBtn;
@property (strong, nonatomic) UILabel *commentLabel;

@property (strong, nonatomic) UIImageView * headImg;
@end

@implementation putDynamicDetailVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self sendRequestWithUser];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"动态详情";
    self.navigationItem.titleView = titleLabel;
//    self.title = @"动态详情";
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //删除按钮
    NSLog(@"userId:%@",self.dataDict[@"userId"]);
    NSLog(@"userid:%@",[USER_DEFAULTS objectForKey:@"userid"]);
    
    if ([[USER_DEFAULTS objectForKey:@"userid"] integerValue] == [self.dataDict[@"userId"] integerValue]) {

        UIBarButtonItem *rightItem = [UIBarButtonItem itemWithTarget:self action:@selector(rightBarButtonItemClick) image:@"删除"];
        self.navigationItem.rightBarButtonItem = rightItem;
        
    }else{
    
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"举报" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
        [rightItem setTintColor:[UIColor whiteColor]];
        self.navigationItem.rightBarButtonItem = rightItem;
    
    }
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    page = 1;
    dataDict = [NSDictionary dictionary];
    
    dataArr = nil;
    
    [self sendRequest];
    
    [self setFootView];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}

-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - 删除发布的信息
- (void)rightBarButtonItemClick{
    
    if ([[USER_DEFAULTS objectForKey:@"userid"] integerValue] == [self.dataDict[@"userId"] integerValue]) {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认删除" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *defaAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteMsg];
    }];
    
    [alert addAction:defaultAction];
    [alert addAction:defaAction];
    
    [self presentViewController:alert animated:YES completion:nil];
        
    }else{
    
        LSReportVCTableViewController * LSReportVCTableView = [[LSReportVCTableViewController alloc]init];
        LSReportVCTableView.resId = self.dataDict[@"Id"];
        LSReportVCTableView.userName = self.dataDict[@"username"];
        [self.navigationController pushViewController:LSReportVCTableView animated:YES];
    
    }
}
-(void)initHeadView{
    
    //头像
    UIImageView * headImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 45, 45)];
    headImg.layer.masksToBounds = YES;
    headImg.layer.cornerRadius = 5.0f;
    [headImg sd_setImageWithURL:[NSURL URLWithString:self.dataDict[@"headPhoto"]]];
    
    self.headImg = headImg;
    
    UIButton * imgBtn = [[UIButton alloc]initWithFrame:headImg.bounds];
    [imgBtn addTarget:self action:@selector(clickImg) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat x;
    
    x = CGRectGetMaxX(headImg.frame) + 5;
    //姓名
    MLLinkLabel * name = [[MLLinkLabel alloc]init];
    name.dataDetectorTypes = MLDataDetectorTypeAll;
    name.allowLineBreakInsideLinks = NO;
    name.linkTextAttributes = nil;
    name.activeLinkTextAttributes = nil;
    name.lineHeightMultiple = UserNickLineHeight;
    name.textColor = [UIColor colorWithRed:45.0/255.0 green:158.0/255.0 blue:222.0/255.0 alpha:1];
    name.adjustsFontSizeToFitWidth = NO;
    name.textInsets = UIEdgeInsetsZero;
    
    NSAttributedString *userNick  = [[NSAttributedString alloc] initWithString:self.dataDict[@"username"]];
    
    CGSize textSize = [MLLinkLabel getViewSize:userNick maxWidth:UserNickMaxWidth font:[UIFont systemFontOfSize:16] lineHeight:UserNickLineHeight lines:2];
    name.frame = CGRectMake(x, headImg.y + 2, textSize.width + 10, textSize.height);
    name.text = self.dataDict[@"username"];
    name.attributedText = userNick;
    
    //职位
    UILabel * positionName = [[UILabel alloc]init];
    
    x = CGRectGetMaxX(name.frame);

    positionName.frame = CGRectMake(x, headImg.y + 2, [UIScreen mainScreen].bounds.size.width - x - 15, textSize.height);
    positionName.font = [UIFont systemFontOfSize:14];
    positionName.text = self.dataDict[@"posiTionName"];
    positionName.textColor = [UIColor lightGrayColor];

    
    //v图标
    UIImageView * vImg = [[UIImageView alloc]initWithFrame:CGRectMake(positionName.x + positionName.text.length * 14 + 3, positionName.y + 1, 15, 15)];
    
//    if ([self.dataDict[@"authState"] isEqualToString:@"2"]) {
//        
//        vImg.image = [UIImage imageNamed:@"yirenzheng"];
//        
//    }else{
//        
//        vImg.image = [UIImage imageNamed:@"weirenzheng"];
//        
//    }
    //会员图标
    vImg.image = [UsedTool setVipimage:self.dataDict[@"vipLevel"]];
    //时间
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(screenW - 120, 20, 100, 15)];
    timeLabel.text = [CustomExcetion prettyDateWithReference:[CustomExcetion dateFromString:self.dataDict[@"createTime"]]];
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.textAlignment = NSTextAlignmentRight;
    timeLabel.textColor = [UIColor lightGrayColor];
    
    //公司名称
    UILabel * companyName = [[UILabel alloc]initWithFrame:CGRectMake(name.x, name.y + name.height + 2, screenW - headImg.width - 50, 20)];
    companyName.textColor = [UIColor lightGrayColor];
    companyName.font = [UIFont systemFontOfSize:14];
    
    if ([self.dataDict[@"userType"] isEqualToString:@"3"]) {
        
        companyName.text = self.dataDict[@"address"];
        
    }else if ([self.dataDict[@"userType"] isEqualToString:@"1"]){
        
        if (![self.dataDict[@"companyUserName"] isEqualToString:@"null"] && ![self.dataDict[@"companyUserName"] isEqualToString:@""]) {
            
            companyName.text = self.dataDict[@"companyUserName"];
            
        }else{
            
            companyName.text = @"";
            
        }
        
    }else{
        
        companyName.text = @"";
        
    }
    
    //文字详情
    UILabel * content = [[UILabel alloc]init];
    content.text = self.dataDict[@"content"];
    content.numberOfLines = 0;
    
    content.textAlignment = NSTextAlignmentLeft;
    UIFont *font = [UIFont fontWithName:@"Arial" size:14];
    content.textColor = [UIColor blackColor];
    content.font = font;
    CGSize constraint = CGSizeMake(screenW - 30, 9000.0f);
    CGSize size = [content.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    [content setFrame:CGRectMake(15, headImg.y + headImg.height + 15, screenW - 30, size.height)];
    
    UIView * headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor whiteColor];
    headView.x = 0;
    headView.y = 0;
    headView.width = screenW;
    
    _bodyView = [[UIView alloc]init];
    
    if (_gridImageView == nil) {
        
        CGFloat x, y , width, height;
        
        x = 0;
        y = 0;
        width = screenW - 40;;
        
        //        height = width;
        height = GridMaxWidth;
        
        _gridImageView = [[DFGridImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self.bodyView addSubview:_gridImageView];
    }
    
    NSMutableArray * imgArr = [NSMutableArray array];

     NSMutableArray * imgArrSec = [NSMutableArray array];
    imgArrSec = [self.dataDict[@"photos"] copy];
    
    for (int i = 0 ; i < imgArrSec.count; i++) {
        
        NSString *str1 = imgArrSec[i];
        
        if (![str1 isEqualToString:Default_Image]) {
            
            [imgArr addObject:imgArrSec[i]];

        }
        
    }
    
    CGFloat gridHeight = [DFGridImageView getHeight:imgArr maxWidth:GridMaxWidth oneImageWidth:screenW - 40 oneImageHeight:imgArr.count/3 * 66.25];
    
    
    CGFloat   width, height;

    width = _gridImageView.frame.size.width;
    
    if (imgArr.count == 1) {
        
//        [_gridImageView updateWithImages:imgArr srcImages:imgArr oneImageWidth:360 oneImageHeight:360];
//        height = 360;
//        
       
            //显示单张图片时，固定高度；
            CGFloat itemH = 160;
            CGFloat itemW = 0;
            UIImageView *imageview = [[UIImageView alloc] init];
            NSURL *url = [NSURL URLWithString:imgArr[0]];
            [imageview sd_setImageWithURL:url];
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
            //判断是否有缓存
            BOOL existBool = [manager diskImageExistsForURL:url];
            UIImage *image;
            if (existBool) {
                image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
            }else{
                NSData *data = [NSData dataWithContentsOfURL:url];
                image = [UIImage imageWithData:data];
            }
            
            //根据image的比例来设置高度
            if (image.size.height) {
                itemW = image.size.width/image.size.height *itemH;
                if(itemW >= itemH){
                    itemW = 160;
                    itemH = image.size.height/image.size.width *itemW;
                }
            }
        [_gridImageView updateWithImages:imgArr srcImages:imgArr oneImageWidth:itemW oneImageHeight:itemH];
        height = itemH;
    
    }else{
        
        [_gridImageView updateWithImages:imgArr srcImages:imgArr oneImageWidth:screenW - 40 oneImageHeight:imgArr.count/3 * 66.25];
        height = gridHeight;
        
    }
    _gridImageView.frame = CGRectMake(_gridImageView.frame.origin.x, 0, width, height);

    _bodyView.x = headImg.x;
    _bodyView.y = content.y + content.height + 10;
    _bodyView.width = screenW;
    
    if (imgArr.count == 1) {
        
        _bodyView.height = 100;
        
    }else{
        
        _bodyView.height = gridHeight + 25;
        
    }
    
    [headView addSubview:_bodyView];
    

    
    if (imgArr.count == 1) {
        
        headView.height = content.y + content.height + headImg.height + _bodyView.height + 100;

    }else{
        
        headView.height = content.y + content.height + headImg.height + _bodyView.height + 30;
       
    }

    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(10,headView.height - 60, screenW - 20, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
//    [headView addSubview:lineView];
    
    UIButton *shareBtn = [[UIButton alloc]init];
    [shareBtn setImage:[UIImage imageNamed:@"分享-商务圈"] forState:0];
    [shareBtn addTarget:self action:@selector(onClickShare) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *zanBtn = [[UIButton alloc]init];
    [zanBtn setImage:[UIImage imageNamed:@"点赞-商务圈"] forState:0];
    [zanBtn setImage:[UIImage imageNamed:@"点赞c"] forState:UIControlStateSelected];
    [zanBtn addTarget:self action:@selector(onClickZan:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *commentBtn = [[UIButton alloc]init];
    [commentBtn setImage:[UIImage imageNamed:@"评论-商务圈"] forState:0];
//    [commentBtn addTarget:self action:@selector(onClickCot:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *shareLabel = [[UILabel alloc]init];
    shareLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:14];
    shareLabel.text = self.dataDict[@"shareCount"];
    
    UILabel *zanLabel = [[UILabel alloc]init];
    zanLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:14];
    zanLabel.text = dataDict[@"zanCount"];
    self.zanLabel = zanLabel;
    
    UILabel *commentLabel = [[UILabel alloc]init];
    commentLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:14];
    commentLabel.text = self.dataDict[@"commentCount"];
    self.commentLabel = commentLabel;
    
    shareBtn.frame = CGRectMake(lineView.x, lineView.y + 1 + 5, 30, 20);
    
    zanBtn.frame = CGRectMake(lineView.centerX - 30/2, lineView.y + 1 + 5, 30, 20);
    self.zanBtn = zanBtn;
    if ([dataDict[@"isZan"] isEqualToString:@"是"]) {
        
        self.zanLabel.textColor = [UIColor orangeColor];
        self.zanBtn.selected = YES;
        
    }else{
        
        self.zanLabel.textColor = [UIColor blackColor];
        self.zanBtn.selected = NO;
        
    }
    
    commentBtn.frame = CGRectMake(lineView.width - 30, lineView.y + 1 + 5 + 2, 30, 20);
    
    shareLabel.frame = CGRectMake(shareBtn.x + shareBtn.width + 4, shareBtn.y, 30, 20);
    self.shareLabel = shareLabel;
    
    self.zanLabel.frame = CGRectMake(zanBtn.x + zanBtn.width + 4, zanBtn.y, 30, 20);
    
    commentLabel.frame = CGRectMake(commentBtn.x + commentBtn.width + 4, commentBtn.y - 1, 30, 20);

    [headView addSubview:headImg];
    [headView addSubview:name];
    [headView addSubview:positionName];
    [headView addSubview:vImg];
    [headView addSubview:timeLabel];
    [headView addSubview:companyName];
    [headView addSubview:content];
//    [headView addSubview:shareBtn];
//    [headView addSubview:shareLabel];
//    [headView addSubview:zanBtn];
//    [headView addSubview:zanLabel];
//    [headView addSubview:commentBtn];
//    [headView addSubview:commentLabel];
    [headView addSubview:imgBtn];
    
    self.tableview.tableHeaderView = headView;

}

-(void)setFootView{

        UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 30)];
        footView.backgroundColor = [UIColor whiteColor];
        UILabel * label = [[UILabel alloc]init];
        label.text = @"暂无评论";
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.frame = CGRectMake(screenW/2 - 40, 0, 80, 25);
        [footView addSubview:label];
        
    self.tableview.tableFooterView = footView;

}

-(void)clickImg{

    NSMutableArray *photos = [NSMutableArray array];
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    MJPhoto *photo = [[MJPhoto alloc] init];
    photo.url = [NSURL URLWithString:self.dataDict[@"headPhoto"]];
    photo.srcImageView = self.headImg;
    [photos addObject:photo];
    browser.photos = photos;
    
    [browser show];

}
#pragma mark - share zan
-(void)onClickShare{

    /**
     * 在简单分享中，只要设置共有分享参数即可分享到任意的社交平台
     **/
    
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
    //    （注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"乐商网"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"https://itunes.apple.com/us/app/le-shang-wang/id1070850954?l=zh&ls=1&mt=8"]
                                          title:@"乐商网"
                                           type:SSDKContentTypeAuto];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
                               dic[@"resId"] = self.dataDict[@"Id"];

                               [LSHttpTool sendHttpData:@"zanCibd_app_cibd.im.lensum" params:dic success:^(id responseObject) {
                                   
                                   NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                                   
                                   if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                                       
                                       NSInteger count = [self.dataDict[@"shareCount"] integerValue] + 1;
                                       self.shareLabel.text = [NSString stringWithFormat:@"%d",count];
                                       
                                   }
                                   
                               } failure:^(NSError *error) {
                                   
                                   
                               }];

                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"确定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           default:
                               break;
                       }
                   }
         ];}


}
-(void)onClickZan:(id)sender{
    if (allConstMemberid) {
        
        self.zanBtn.selected = !self.zanBtn.selected;
        
        if (self.zanBtn.selected) {
            
            self.zanLabel.textColor = [UIColor orangeColor];
            type = 1;
            [self zanSendRequest];
            
            NSInteger count = [self.zanLabel.text integerValue] + 1;
            self.zanLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
            
            
        }else{
            
            self.zanLabel.textColor = [UIColor blackColor];
            type = 2;
            [self zanSendRequest];
            
            NSInteger count = [self.zanLabel.text integerValue] - 1;
            self.zanLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
            
        }
        
    }else{
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
        [alert show];
        
        
    }
    
}
#pragma mark - alertview
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //登录
        UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *loginNAV = [LoginAndRegister instantiateViewControllerWithIdentifier:@"LoginNav"];
        [self presentViewController:loginNAV animated:YES completion:nil];
    }
    
    
}
-(void)zanSendRequest{
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    dic[@"userId"] = allConstMemberid;
    dic[@"token"] = allConstMemberToken;
    dic[@"resId"] = self.dataDict[@"Id"];
    dic[@"type"] = @(type);

    [LSHttpTool sendHttpData:@"zanCibd_app_cibd.im.lensum" params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] != 1) {

            [self showSingleIconBubble:dicInfo[@"msg"]];
            
            }
        
        [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
    }];
    
    
}
#pragma mark - datasource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    projectCell *cell = [self tableView:self.tableview cellForRowAtIndexPath:indexPath];
    
    cell.commentLabel.numberOfLines = 0;
    
    cell.commentLabel.textAlignment = NSTextAlignmentLeft;
    UIFont *font = [UIFont fontWithName:@"Arial" size:12];
    cell.commentLabel.textColor = [UIColor blackColor];
    cell.commentLabel.font = font;
    CGSize constraint = CGSizeMake(cell.width - 53, 9000.0f);
    CGSize size = [cell.commentLabel.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    [cell.commentLabel setFrame:CGRectMake(53, 45, cell.width - 53, size.height)];
    
    //设置cell自适应高度
    CGRect cellFrame = [cell frame];
    cellFrame.origin = CGPointMake(0, 0);
    CGFloat totalHeight = cellFrame.size.height+cell.commentLabel.height;
    cellFrame.size.height = totalHeight;
    [cell setFrame:cellFrame];

    return cell.frame.size.height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    NSLog(@"-----------------%@",self.positionArr);
//    // 注册
    UINib * nib = [UINib nibWithNibName:@"projectCell" bundle:nil];
    [self.tableview registerNib:nib forCellReuseIdentifier:projectcell];
    
    projectCell * cell = [self.tableview dequeueReusableCellWithIdentifier:projectcell];
    cell.lineview.hidden = YES;
    if (!cell) {
        cell = [[projectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:projectcell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    
    NSDictionary *dict = dataArr[indexPath.row];
    
    [cell.headPhoto sd_setImageWithURL:dict[@"headPhoto"] placeholderImage:[UIImage imageNamed:@"20100403114758394_4147"]];
    
    cell.headPhoto.layer.masksToBounds = YES;
    cell.headPhoto.layer.cornerRadius = 20.0f;
    
    cell.commentUsername.textColor = [UIColor colorWithRed:23.0/255.0 green:176.0/255.0 blue:217.0/255.0 alpha:1];
    
    if ([dict[@"commentUsername"] isEqualToString:@"null"] || [dict[@"commentUsername"] isEqualToString:@""]) {
        cell.commentUsername.text = dict[@"commentAccount"];
    }else{
        cell.commentUsername.text = dict[@"commentUsername"];
    }
    
    NSDate *date = [CustomExcetion dateFromString:dict[@"createTime"]];
    cell.createTime.text = [CustomExcetion prettyDateWithReference:date];
    
    cell.commentLabel.text = dict[@"content"];
    
    if ([dict[@"userType"] isEqualToString:@"3"]) {
        cell.posiTion.text = @"";
        
        if ([dict[@"goveUserName"] isEqualToString:@"null"]) {
            cell.companyUserName.text = @"";
        }else{
            cell.companyUserName.text = dict[@"goveUserName"];
        }
    }else if ([dict[@"userType"] isEqualToString:@"2"]){
        
        if ([dict[@"goveUserName"] isEqualToString:@"null"] && [dict[@"companyUserName"] isEqualToString:@"null"]) {
            
            cell.companyUserName.text = @"";
            
        }else if ([dict[@"goveUserName"] isEqualToString:@"null"]){
            
            cell.companyUserName.text = dict[@"companyUserName"];
            
        }else if ([dict[@"companyUserName"] isEqualToString:@"null"]){
            
            cell.companyUserName.text = dict[@"goveUserName"];
            
        }
        
    }else{
    
        if ([dict[@"goveUserName"] isEqualToString:@"null"] && [dict[@"companyUserName"] isEqualToString:@"null"]) {
            
            cell.companyUserName.text = @"";
            
        }else if ([dict[@"goveUserName"] isEqualToString:@"null"]){
            
            cell.companyUserName.text = dict[@"companyUserName"];
            
        }else if ([dict[@"companyUserName"] isEqualToString:@"null"]){
            
            cell.companyUserName.text = dict[@"goveUserName"];
            
        }

        cell.posiTion.text = dict[@"posiTion"];
    
    }
    
    return cell;
    
    
}


-(void) loadMore
{
    page ++;
    //加载更多
    [self sendRequest];
}

-(void)sendRequest{

    //加载更多
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    dic[@"id"] = self.dataDict[@"Id"];
    dic[@"page"] = @(page);

    [LSHttpTool sendHttpData:@"getCommentList_app_cibd.im" params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];

        //
//                NSString * jsonStr = [LSHttpTool DataTOjsonString:dicInfo];
//                NSLog(@"--------------------json:%@",jsonStr);
        
        
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            
            if (page == 1) {
                dataArr = nil;
                dataArr = dicInfo[@"data"];
                
                
            }else{
                NSArray *arr = dicInfo[@"data"];
                
                NSMutableArray * mtArr = [NSMutableArray array];
                for (NSDictionary * dict in dataArr) {
                    [mtArr addObject:dict];
                }
                for (NSDictionary * dict in arr) {
                    [mtArr addObject:dict];
                }
                dataArr = nil;
                dataArr = mtArr;
                
            }
            
            [self.tableview.tableFooterView removeFromSuperview];
            
            // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
            MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
            
            // 设置刷新图片
            UIImage *images = [UIImage imageNamed:@"mj_footer"];
            NSArray *refreshingImages = @[images];
            [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
            [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
            footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
            footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
            
            [footer setAutomaticallyRefresh:NO];
            
            self.tableview.mj_footer = footer;
            
            [self.tableview reloadData];
            
        }else{
            
            [self.tableview reloadData];

        }
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
    }];
    
    [self.tableview.mj_header endRefreshing];
}

-(void)sendRequestWithUser{
    
    self.tableview.tableHeaderView = nil;
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    dic[@"userId"] = allConstMemberid;
    dic[@"id"] = self.dataDict[@"Id"];

    [LSHttpTool sendHttpData:@"getCibdDetail_app_cibd.im" params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            dataDict = dicInfo[@"data"];
            [self initHeadView];
            
        }else{

        }
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        
    }];

}
- (IBAction)sendText:(UITextField *)sender {
    

    if (allConstMemberid) {
        
        if (self.textfiled.text.length != 0) {
            
            [self showRoundProgressWithTitle:@"请稍后..."];
            
            NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
            [dic setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
            [dic setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
            [dic setObject:allConstMemberid forKey:@"userId"];
            [dic setObject:allConstMemberToken forKey:@"token"];
            [dic setObject:self.dataDict[@"Id"] forKey:@"resId"];
            [dic setObject:self.textfiled.text forKey:@"content"];
            
            dataDict = [NSDictionary dictionary];

            [LSHttpTool sendHttpData:@"commentCibd_app_cibd.im.lensum" params:dic success:^(id responseObject) {
                
                NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                
                if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                    
                    dataArr = dicInfo[@"data"];
                    self.commentLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)dataArr.count];
                    
                }else{

                    [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
                    
                }
                
                [self.textfiled resignFirstResponder];
//                [self sendRequestWithUser];
                [self.tableview reloadData];
                self.textfiled.text = @"";
                
                [self hideBubble];
                
            } failure:^(NSError *error) {

                [self showSingleIconBubble:@"网络连接错误"];
                
            }];
            
            
        }else{

        }
        
    }else{
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
        [alert show];
        
    }
    
}

//删除发布的信息
- (void)deleteMsg{
    //http://192.168.1.188:8080/lensum/deleteBid_app_cibd.im.lensum?token=EhnDuxqM6n8u9OIIuwcT&resId=1
    NSMutableDictionary *mudic = [NSMutableDictionary dictionary];
    [mudic setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];
    [mudic setObject:self.dataDict[@"Id"] forKey:@"resId"];
    [LSHttpTool sendHttpData:@"deleteBid_app_cibd.im.lensum" params:mudic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([dicInfo[@"result"] integerValue] == 1) {

            [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];
        }else{

            [self showSingleIconBubble:dicInfo[@"msg"]];
        }

        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {

        [self showSingleIconBubble:error.localizedDescription];

    }];
}


//放弃第一响应者
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
//    [self.textfiled resignFirstResponder];
}
@end
